//
//  BaseViewController.m
//  DemoProject
//
//  Created by liu xiwang on 2018/4/10.
//  Copyright © 2018年 Sunqk. All rights reserved.
//

#import "BaseViewController.h"
#import "WRNavigationBar.h"
#import "SunAppDelegate.h"
#define KLoadDataBase        @"LoadDataWithNetWorkChanged"
@interface BaseViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) BOOL isLoadedData;

@property (nonatomic, getter=isUseCustomNav) BOOL useCustomNav;

@end

@implementation BaseViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    //网络监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getLoadDataBase:) name:KLoadDataBase object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:_useCustomNav animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter ]removeObserver:self name:KLoadDataBase object:nil];
}

#pragma mark   Method

- (void)getLoadDataBase:(NSNotification *)text{
    if (_isLoadedData) {
        return;
    }
    NSLog(@"开始重新加载网络");
}

- (void)setupNavBar{
    [self.view addSubview:self.customNavBar];
    // 设置自定义导航栏背景图片
    self.customNavBar.barBackgroundImage = [UIImage imageNamed:@"millcolorGrad"];
    // 设置自定义导航栏标题颜色
    self.customNavBar.titleLabelColor = [UIColor whiteColor];
    if (self.navigationController.childViewControllers.count != 1) {
        [self.customNavBar wr_setLeftButtonWithTitle:@"<<" titleColor:[UIColor whiteColor]];
    }
}

//dimiss页面
- (void)dismissVC{
    [self dismissViewControllerAnimated:YES completion:nil];
    self.tabBarController.tabBar.hidden = NO;
}

//返回任意一个页面
- (void)popToViewControllerAtIndex:(NSInteger)index{
    if (self.navigationController.viewControllers.count > index) {
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index] animated:YES];
    }
}

//popToRoot页面
- (void)popToRoot{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//pop页面
- (void)popVC{
    [self.navigationController popViewControllerAnimated:YES];
}

//push页面
- (void)dsPushViewController:(UIViewController*)vc animated:(BOOL)animated{
    if (self.navigationController.viewControllers.count == 1) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    [self.navigationController pushViewController:vc animated:animated];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)hideKeyBoard{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}


#pragma mark   geter/setter

-(void)setUseCustomNav:(BOOL)useCustomNav{
    if (self.isUseCustomNav == useCustomNav) {
        return;
    }
    [self setupNavBar];
    _useCustomNav = useCustomNav;
}

- (WRCustomNavigationBar *)customNavBar{
    if (_customNavBar == nil) {
        _customNavBar = [WRCustomNavigationBar CustomNavigationBar];
    }
    return _customNavBar;
}

-(SUNTableView *)tableView{
    if (!_tableView) {
        _tableView = [[SUNTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

-(void)setIsLoadedData:(BOOL)isLoadedData{
    if (self.isLoadedData == isLoadedData) {
        return;
    }
    _isLoadedData = isLoadedData;
}
@end
